package com.ruoyi.framework.security;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @ClassName CustomUsernamePasswordAuthenticationToken
 * @Description 描述
 * @Author HanWL
 * @Date 2023/10/16 20:30
 * @Version 1.0
 **/

public class CustomUsernamePasswordAuthenticationToken extends UsernamePasswordAuthenticationToken {

    private static final long serialVersionUID = 520L;
    private final Object principal;
    private Object credentials;
    private final String platform;

    public CustomUsernamePasswordAuthenticationToken(Object principal, Object credentials, String platform) {
        super(principal, credentials);
        this.principal = principal;
        this.credentials = credentials;
        this.platform = platform;
    }


    public CustomUsernamePasswordAuthenticationToken(Object principal, Object credentials, String platform,  Collection<? extends GrantedAuthority> authorities) {
        super(principal,credentials,authorities);
        this.principal = principal;
        this.credentials = credentials;
        this.platform = platform;
        super.setAuthenticated(true);
    }

    public Object getCredentials() {
        return this.credentials;
    }

    public Object getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    public void eraseCredentials() {
        super.eraseCredentials();
        this.credentials = null;
    }
    public String getPlatform() {
        return platform;
    }
}
